//
//  FKLiveCell.m
//  LiveDemo1
//
//  Created by xmc06 on 16/10/13.
//  Copyright © 2016年 zmosa. All rights reserved.
//

#import "FKLiveCell.h"
#import "FKLiveItem.h"
#import "FKCreatorItem.h"
#import <UIImageView+WebCache.h>
#define FKColor(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define FKScreenW [UIScreen mainScreen].bounds.size.width
#define FKScreenH [UIScreen mainScreen].bounds.size.height
static NSString *const liveCell = @"liveCell";

@interface FKLiveCell ()
@property (nonatomic, weak) UIImageView *avatarView; // 头像
@property (nonatomic, weak) UILabel *nameLabel; // 直播名
@property (nonatomic, weak) UILabel *addressLabel; // 地址
@property (nonatomic, weak) UILabel *playNumLabel; // 观看人数
@property (nonatomic, weak) UILabel *liveLabel; // 直播
@property (nonatomic, weak) UIImageView *bigPicView; // 大图
@end

@implementation FKLiveCell

+ (instancetype)liveCellWithTableView:(UITableView *)tableView
{
    FKLiveCell *cell = [tableView dequeueReusableCellWithIdentifier:liveCell];
    if (!cell) {
        cell = [[FKLiveCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:liveCell];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews
{
    UIImageView *avatarView = [[UIImageView alloc] init];
    [self.contentView addSubview:avatarView];
    self.avatarView = avatarView;
    avatarView.layer.cornerRadius = 5;
    avatarView.layer.masksToBounds = true;
    
    UILabel *nameLabel = [[UILabel alloc] init];
    [self.contentView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    nameLabel.font = [UIFont systemFontOfSize:14];
    nameLabel.textColor = FKColor(206, 206, 206);
    
    UILabel *addressLabel = [[UILabel alloc] init];
    [self.contentView addSubview:addressLabel];
    self.addressLabel = addressLabel;
    addressLabel.font = [UIFont systemFontOfSize:14];
    addressLabel.textColor = FKColor(206, 206, 206);
    
    UILabel *playNumLabel = [[UILabel alloc] init];
    [self.contentView addSubview:playNumLabel];
    self.playNumLabel = playNumLabel;
    playNumLabel.font = [UIFont systemFontOfSize:14];
    
    UIImageView *bigPicView = [[UIImageView alloc] init];
    [self.contentView addSubview:bigPicView];
    self.bigPicView = bigPicView;
    
    UILabel *liveLabel = [[UILabel alloc] init];
    [self.contentView addSubview:liveLabel];
    self.liveLabel = liveLabel;
    liveLabel.font = [UIFont systemFontOfSize:14];
    liveLabel.text = @"直播";
    liveLabel.layer.cornerRadius = 5;
    liveLabel.layer.masksToBounds = true;
}

/**
 *  布局子控件
 */
- (void)layoutSubviews
{
    [super layoutSubviews];
    
//    self.avatarView.frame = CGRectMake(10, 5, 40, 40);
    
    CGFloat bigPicViewY = CGRectGetMaxY(self.avatarView.frame) + 5;
    self.bigPicView.frame = CGRectMake(0, bigPicViewY, FKScreenW, self.contentView.frame.size.height - bigPicViewY);
}

- (void)setLive:(FKLiveItem *)live
{
    _live = live;
    
    [self setupData:live];
    [self setupFrame];
}

- (void)setupData:(FKLiveItem *)live
{
    NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://img.meelive.cn/%@", live.creator.portrait]];
    [self.avatarView sd_setImageWithURL:imageURL placeholderImage:nil options:SDWebImageRefreshCached];
    if (live.city.length == 0) {
        self.addressLabel.text = @"难道在火星?";
    } else {
        self.addressLabel.text = live.city;
    }
    
    self.nameLabel.text = live.creator.nick;
    [self.bigPicView sd_setImageWithURL:imageURL placeholderImage:nil];
    
    // 设置当前观众数量
    NSString *fullChaoyang = [NSString stringWithFormat:@"%zd人在看", live.online_users];
    NSRange range = [fullChaoyang rangeOfString:[NSString stringWithFormat:@"%zd", live.online_users]];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:fullChaoyang];
    [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:range];
    [attr addAttribute:NSForegroundColorAttributeName value:FKColor(216, 41, 116) range:range];
    self.playNumLabel.attributedText = attr;
}

- (void)setupFrame
{
    self.avatarView.frame = CGRectMake(10, 5, 40, 40);
    
    CGFloat nameLabelX = CGRectGetMaxX(self.avatarView.frame) + 10;
    CGFloat nameLabelY = self.avatarView.frame.origin.y;
    CGSize nameLabelSize = [self.nameLabel.text sizeWithAttributes:@{NSFontAttributeName : self.nameLabel.font}];
    self.nameLabel.frame = (CGRect){{nameLabelX, nameLabelY}, nameLabelSize};
    
    CGFloat addressLabelX = nameLabelX;
    CGSize addressLabelSize = [self.addressLabel.text sizeWithAttributes:@{NSFontAttributeName : self.addressLabel.font}];
    CGFloat addressLabelY = CGRectGetMaxY(self.avatarView.frame) - addressLabelSize.height;
    self.addressLabel.frame = (CGRect){{addressLabelX, addressLabelY}, addressLabelSize};
    
    CGSize playNumLabelSize = [self.playNumLabel.text sizeWithAttributes:@{NSFontAttributeName : self.playNumLabel.font}];
    CGFloat playNumLabelX = FKScreenW - playNumLabelSize.width - 10;
    CGFloat playNumLabelY = self.avatarView.center.y - (playNumLabelSize.height * 0.5);
    self.playNumLabel.frame = (CGRect){{playNumLabelX, playNumLabelY}, playNumLabelSize};
    
//    CGFloat bigPicViewY = CGRectGetMaxY(self.avatarView.frame) + 5;
//    self.bigPicView.frame = CGRectMake(0, bigPicViewY, FKScreenW, self.contentView.frame.size.height - bigPicViewY);
    
//    [self setNeedsDisplay];
//    [self setNeedsLayout];
}

@end
